<?php
namespace Kuoqi;
use Kuoqi\FileDriver;
/**
 * Class Cache
 * @package Kuoqi
 * User kuoqi
 * Datetime 2022/2/28 16:37
 * Remark 缓存基础模块
 */
class Cache{


    /**
     * @param $stream 待存储的文件数据
     * @return Cache
     * User kuoqi
     * Datetime 2022/2/28 16:38
     * Remark 快速设置一个模板
     */
    static public function setTemplate($stream){
        $fn = md5($stream);
        $file = new FileDriver();
        $file->setDirname(Params::$DS . Params::$RUNTIME_PATH . Params::$DS . Params::$TEMPLATE_PATH);
        $file->setBasename($fn.'.cache');
        $file->setContent($stream);
        if(!$file->save()){
            Error::process('配置模板'.$file->getFile().'失败');
        }
        return $fn;
    }

    /**
     * @param $tpid
     * @param $args
     * @return string
     * @throws Exception
     * User kuoqi
     * Datetime 2022/2/28 18:04
     * Remark 快速获取一个模板信息
     */
    static public function getTemplate($tpid,$args){
        $file = new FileDriver();
        $file->setDirname(Params::$DS . Params::$RUNTIME_PATH . Params::$DS . Params::$TEMPLATE_PATH);
        $file->setBasename($tpid.'.cache');
        return $file->readWithphp($args);
    }

    /**
     * @param $name
     * @param $code
     * @param $exp_time
     * @param $cpath
     * @param $isCommon
     * @return string
     * User kuoqi
     * Datetime 2022/3/2 21:38
     * Remark 目前使用公共存取
     */
    static public function setCache($name,$code,$exp_time,$cpath,$isCommon){
        $fn = md5($name);
        $file = new FileDriver();
        if($cpath){
            $file->setDirname(\Kuoqi\FileDriver\Path::getRuntime($isCommon,$cpath));
        }else{
            $file->setDirname(\Kuoqi\FileDriver\Path::getCache($isCommon));
        }
        $file->setBasename($fn.'.cache');
        $file->setContent(serialize($code).'::::::'.(time()+intval($exp_time)));
        if(!$file->save()){
            Error::process('缓存文件'.$file->getFile().'配置失败');
        }
        return $fn;
    }

    /**
     * @param $name
     * @param $cpath
     * @param $isCommon
     * @return mixed|null
     * User kuoqi
     * Datetime 2022/3/2 21:54
     * Remark 获取缓存信息
     */
    static public function getCache($name,$cpath,$isCommon){
        $fn = md5($name);
        $file = new FileDriver();
        if($cpath){
            $file->setDirname(\Kuoqi\FileDriver\Path::getRuntime($isCommon,$cpath));
        }else{
            $file->setDirname(\Kuoqi\FileDriver\Path::getCache($isCommon));
        }
        $file->setBasename($fn.'.cache');
        $code = $file->getContent();
        $tmp = explode('::::::',$code);
        if(count($tmp)==2){
            $time = $tmp[1];
            if($time>time()){
                return  unserialize($code);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    /**
     * @param $name
     * @param $cpath
     * @param $isCommon
     * @return bool
     * User kuoqi
     * Datetime 2022/3/3 14:15
     * Remark 删除一个缓存信息
     */
    static public function delCache($name,$cpath,$isCommon){
        $fn = md5($name);
        $file = new FileDriver();
        if($cpath){
            $file->setDirname(\Kuoqi\FileDriver\Path::getRuntime($isCommon,$cpath));
        }else{
            $file->setDirname(\Kuoqi\FileDriver\Path::getCache($isCommon));
        }
        $file->setBasename($fn.'.cache');
        return $file->delete();
    }

}
